package com.jianggy.stream;

import javax.swing.table.TableRowSorter;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author jianggy
 * @version 1.0
 * @time 2019/11/18 15:16
 * @see
 **/
public class FirstDemo {
    public static void main(String[] args) {
        List<Integer> myList = new ArrayList<>();
        myList.add(7);
        myList.add(18);
        myList.add(10);
        myList.add(24);
        myList.add(17);
        myList.add(5);
        System.out.println("original list: " + myList);
        Stream<Integer> stream = myList.stream();
        //minimum
        Optional<Integer> minValue = stream.min(Integer::compareTo);
        if (minValue.isPresent()) {
            System.out.println("minimum value: " + minValue.get());
        }
        //maximum
        Optional<Integer> maxValue = myList.stream().max(Integer::compareTo);
        if (maxValue.isPresent()) {
            System.out.println("maximum value: " + maxValue.get());
        }
        //sort
        Stream<Integer> sortedStream = myList.stream().sorted();
        System.out.print("sorted stream: ");
        sortedStream.forEach((n) -> System.out.print(n + " "));
        System.out.println();
        //odd
        Stream<Integer> oddValues = myList.stream().sorted().filter((n) -> (n % 2) == 1);
        System.out.print("odd values: ");
        oddValues.forEach((n) -> System.out.print(n + " "));
        System.out.println();
        // odd value that are greater than 5.
        oddValues = myList.stream().sorted().filter((n) -> (n % 2) == 1).filter((n) -> n > 5);
        System.out.print("odd values greater than 5: ");
        oddValues.forEach((n) -> System.out.print(n + " "));
        System.out.println();


    }
}
